package com.ruoyi.project.business.pickCode.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.pickCode.mapper.YlPickCodeMapper;
import com.ruoyi.project.business.pickCode.domain.YlPickCode;
import com.ruoyi.project.business.pickCode.service.IYlPickCodeService;
import com.ruoyi.project.business.product.domain.YlProduct;
import com.ruoyi.project.business.product.service.IYlProductService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;

import com.ruoyi.common.utils.text.Convert;

/**
 * 取货码Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-26
 */
@Service
public class YlPickCodeServiceImpl implements IYlPickCodeService 
{
    @Autowired
    private YlPickCodeMapper ylPickCodeMapper;
    @Autowired
    private IYlProductService productService;
    /**
     * 查询取货码
     * 
     * @param pickId 取货码ID
     * @return 取货码
     */
    @Override
    public YlPickCode selectYlPickCodeById(Long pickId)
    {
        return ylPickCodeMapper.selectYlPickCodeById(pickId);
    }

    /**
     * 查询取货码列表
     * 
     * @param ylPickCode 取货码
     * @return 取货码
     */
    @Override
    public List<YlPickCode> selectYlPickCodeList(YlPickCode ylPickCode)
    {
        return ylPickCodeMapper.selectYlPickCodeList(ylPickCode);
    }

    /**
     * 新增取货码
     * 
     * @param ylPickCode 取货码
     * @return 结果
     */
    @Override
    public int insertYlPickCode(YlPickCode ylPickCode)
    {
        ylPickCode.setCreateTime(DateUtils.getNowDate());
        return ylPickCodeMapper.insertYlPickCode(ylPickCode);
    }

    /**
     * 修改取货码
     * 
     * @param ylPickCode 取货码
     * @return 结果
     */
    @Override
    public int updateYlPickCode(YlPickCode ylPickCode)
    {
        ylPickCode.setUpdateTime(DateUtils.getNowDate());
        return ylPickCodeMapper.updateYlPickCode(ylPickCode);
    }

    /**
     * 删除取货码对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteYlPickCodeByIds(String ids)
    {
        return ylPickCodeMapper.deleteYlPickCodeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除取货码信息
     * 
     * @param pickId 取货码ID
     * @return 结果
     */
    @Override
    public int deleteYlPickCodeById(Long pickId)
    {
        return ylPickCodeMapper.deleteYlPickCodeById(pickId);
    }
    
    public int generateYlPickCode(YlPickCode ylPickCode) {
    	ylPickCode.setPickCode(RandomUtil.randomLong(1000000000, 9999999999L));
    	return insertYlPickCode(ylPickCode);
    }
    
    /**
     * 批量停用取货码对象
     * 
     * @param ids 需要停用的数据ID
     * @return 结果
     */
    @Override
    public int blockYlPickCodeByIds(String ids)
    {
    	String[] idArray = Convert.toStrArray(ids);
    	for(String idTemp:idArray) {
    		Long pickId = Long.valueOf(idTemp);
    		blockYlPickCodeById(pickId);
    	}
    	
        return 1;
    }
    
    /**
     * 根据ID停用取货码
     * 
     * @param ids 需要停用的数据ID
     * @return 结果
     */
    @Override
    public int blockYlPickCodeById(Long id)
    {
    	YlPickCode ylPickCode = ylPickCodeMapper.selectYlPickCodeById(id);
    	if(ylPickCode.getStatus().longValue()==1) {
    		ylPickCode.setStatus(0L);
    		ylPickCodeMapper.updateYlPickCode(ylPickCode);
    	}
        return 1;
    }
    
    /**
     * 根据批次ID停用取货码
     * 
     * @param ids 需要停用的批次ID
     * @return 结果
     */
    @Override
    public int blockYlPickCodeByBatchId(Long batchId) {
    	YlPickCode ylPickCode = new YlPickCode();
    	ylPickCode.setBatchId(batchId);
    	List<YlPickCode> pickCodes = ylPickCodeMapper.selectYlPickCodeList(ylPickCode);
    	for(YlPickCode pickCode:pickCodes) {
    		blockYlPickCodeById(pickCode.getPickId());
    	}
    	return 1;
    }
    
    /**
     * 批量启用取货码对象
     * 
     * @param ids 需要启用的数据ID
     * @return 结果
     */
    @Override
    public int activeYlPickCodeByIds(String ids)
    {
    	String[] idArray = Convert.toStrArray(ids);
    	for(String idTemp:idArray) {
    		Long pickId = Long.valueOf(idTemp);
    		activeYlPickCodeById(pickId);
    	}
    	
        return 1;
    }
    
    /**
     * 根据ID启用取货码
     * 
     * @param ids 需要启用的数据ID
     * @return 结果
     */
    @Override
    public int activeYlPickCodeById(Long id)
    {
    	YlPickCode ylPickCode = ylPickCodeMapper.selectYlPickCodeById(id);
    	if(ylPickCode.getStatus().longValue()==0) {
    		ylPickCode.setStatus(1L);
    		ylPickCodeMapper.updateYlPickCode(ylPickCode);
    	}
        return 1;
    }
    
    /**
     * 根据批次ID启用取货码
     * 
     * @param ids 需要启用的批次ID
     * @return 结果
     */
    @Override
    public int activeYlPickCodeByBatchId(Long batchId) {
    	YlPickCode ylPickCode = new YlPickCode();
    	ylPickCode.setBatchId(batchId);
    	List<YlPickCode> pickCodes = ylPickCodeMapper.selectYlPickCodeList(ylPickCode);
    	for(YlPickCode pickCode:pickCodes) {
    		activeYlPickCodeById(pickCode.getPickId());
    	}
    	return 1;
    }
    
    //验证取货码
    public String validatePickCode(String machineId,Long pickCode) {
    	String lastResult = null;
    	String result = null;
    	YlPickCode ylPickCode = new YlPickCode();
    	ylPickCode.setMachineId(machineId);
    	ylPickCode.setPickCode(pickCode);
    	List<YlPickCode> pickCodes = ylPickCodeMapper.selectYlPickCodeList(ylPickCode);
    	for(YlPickCode tmpPickCode:pickCodes) {
    		YlProduct product = productService.selectYlProductById(tmpPickCode.getProductId());
    		if(product.getStatus().compareTo(0L)==0) {
    			result = "NG"+product.getProductName()+"已停用";
    		}
    		if(tmpPickCode.getStatus().compareTo(2L)==0) {
    			result = "NG该取货码已使用";
    		}
    		if(tmpPickCode.getStatus().compareTo(3L)==0) {
    			result = "NG该取货码已被停用";
    		}
    		if(result==null&&tmpPickCode.getEffectiveEndDate()!=null) {
    			if(new Date().getTime()>DateUtil.endOfDay(tmpPickCode.getEffectiveEndDate()).getTime()) {
    				result = "NG该取货码已过有效期";
    			}
    		}
    		if(result==null&&tmpPickCode.getEffectiveStartDate()!=null) {
    			if(new Date().getTime()<DateUtil.beginOfDay(tmpPickCode.getEffectiveStartDate()).getTime()) {
    				result = "NG该取货码未到可使用日期";
    			}
    		}
    		if(tmpPickCode.getStatus().compareTo(1L)==0&&result==null) {
    			result = "OK"+tmpPickCode.getProductName();
    		}
    		if(result!=null&&result.startsWith("OK")) {
    			return result;
    		}
    		lastResult = result;
    		result = null;
    	}
    	if(lastResult==null) {
    		result="NG未找到取货码";
    	}
    	return result;
    }
}
